package com.hc.util;

import com.hc.entity.Environmentmv;
import com.hc.model.QcEquipmentRecordModel;
import com.hc.model.QualityControlReport;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author LiuZhiHao
 * @date 2020/9/9 15:04
 * 描述:
 **/
public class EquipmentRecordServiceImplUtil {


    /**
     * 给环境巡检中的QualityControlReport对象设置属性值
     *
     * @param list
     * @param env
     * @param index 下标
     */
    public static void setEnvProperties(List<QualityControlReport> list, Environmentmv env, int index) {
        Double meanValue = env.getMeanValue();
        if (meanValue != null) {
            list.get(index).setEnvironmentTemperature(meanValue.toString());
        }
        Double cdMv = env.getCdMv();
        if (cdMv != null) {
            list.get(index).setEnvironmentCarbonDioxideConcentration(cdMv.toString());
        }
        Double humidityMv = env.getHumidityMv();
        if (humidityMv != null) {
            list.get(index).setEnvironmentHumidity(humidityMv.toString());
        }
        list.get(index).setEnvironmentMethanal(env.getFdMv());
        Double oxygenMv = env.getOxygenMv();
        if (oxygenMv != null) {
            list.get(index).setEnvironmentOxygenConcentration(oxygenMv.toString());
        }
        Double pm2Mv = env.getPm2Mv();
        if (pm2Mv != null) {
            list.get(index).setEnvironmentPM2(pm2Mv.toString());
        }
        Double pm10Mv = env.getPm10Mv();
        if (pm10Mv != null) {
            list.get(index).setEnvironmentPM10(pm10Mv.toString());
        }
        list.get(index).setEnvironmentPressure(env.getPressureMv());
        list.get(index).setEnvironmentVOC(env.getVocMv());
    }

    /**
     * 计算两个时间之间的所有字符串拼接的日期，返回到一个list<String>集合中
     *
     * @param startTime yyyy-MM-dd
     * @param endTime   yyyy-MM-dd
     * @return
     */
    public static List<String> timeDifferenceCount(String startTime, String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<String> list = new ArrayList<>();
        list.add(startTime);
        try {
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);
            Calendar calendarStart = Calendar.getInstance();
            Calendar calendarEnd = Calendar.getInstance();
            calendarStart.setTime(startDate);
            calendarEnd.setTime(endDate);
            boolean flag = true;
            int count = 0;
            while (flag) {
                count++;
                calendarStart.add(Calendar.DATE, 1);
                Date time = calendarStart.getTime();
                String format = sdf.format(time);
                if (StringUtils.equals(endTime, format)) {
                    flag = false;
                }
                if (count == 32) {
                    flag = false;
                }
                list.add(format);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            System.out.println("时间格式转换异常");
        }
        return list;
    }


    /**
     * 根据环境的上下班状态设置属性
     *
     * @param qcr
     * @param createTime
     * @param list
     */
    public static void determineSetPropertiesForHJ(QcEquipmentRecordModel qcr, Date createTime, List<QualityControlReport> list) {
        String workStatus = qcr.getWorkStatus();
        if (StringUtils.equals("1", workStatus)) {
            for (int k = 1; k <= 4; k++) {
                if (isEffectiveDate(createTime, k, "环境")) {
                    //表示上班巡检
                    list.get(k - 1).setIdOnWork(String.valueOf(qcr.getId()));
                    list.get(k - 1).setInspectionTimeOnWork(qcr.getCreateTime());
                    list.get(k - 1).setRunningStatusOnWork(qcr.getRunningStatus());
                    list.get(k - 1).setCheckingPeopleOnWork(qcr.getCreateBy());
                }
            }
        } else if (StringUtils.equals("2", workStatus)) {
            for (int n = 1; n <= 4; n++) {
                if (isEffectiveDate(createTime, n, "环境")) {
                    //表示下班巡检
                    list.get(n - 1).setIdOffWork(qcr.getId());
                    list.get(n - 1).setInspectionTimeOffWork(qcr.getCreateTime());
                    list.get(n - 1).setRunningStatusOffWork(qcr.getRunningStatus());
                    list.get(n - 1).setCheckingPeopleOffWork(qcr.getCreateBy());
                    list.get(n - 1).setIsGroundClean(qcr.getIsGroundClean());
                }
            }
        }
    }

    /**
     * 根据冰箱上下班状态设置属性，1号5号冰箱有冷冻温度，其他冰箱没有冷冻温度
     *
     * @param qcr        QcEquipmentRecordModel对象
     * @param createTime 创建时间
     */
    public static void determineSetPropertiesForBX(QcEquipmentRecordModel qcr, Date createTime, List<QualityControlReport> list) {
        //上班状态
        if (StringUtils.equals("1", qcr.getWorkStatus())) {
            for (int k = 1; k <= 8; k++) {
                if (isEffectiveDate(createTime, k, "冰箱")) {
                    list.get(k - 1).setTime((k - 1) * 3 + "-" + k * 3);
                    //设备上班巡检的运行状态
                    list.get(k - 1).setRunningStatusOnWork(qcr.getRunningStatus());
                    //上班巡检的创建时间
                    list.get(k - 1).setInspectionTimeOnWork(qcr.getCreateTime());
                    //上班巡检的巡检人
                    list.get(k - 1).setCheckingPeopleOnWork(qcr.getCreateBy());
                    //上班巡检的冷藏温度
                    list.get(k - 1).setColdTemperatureOnWork(qcr.getColdTemperature());
                    //如果是一号冰箱和5号冰箱，设置冷冻温度，其他冰箱没有冷冻温度
                    if (StringUtils.equals("1号冰箱", qcr.getDeviceName()) ||
                            StringUtils.equals("海尔医用冷藏冷冻箱5", qcr.getDeviceName())) {
                        //上班巡检的冷冻温度
                        list.get(k - 1).setFreezingTemperatureOnWork(qcr.getFreezingTemperature());
                    }
                    //将当前qcr对象的巡检记录id赋值给QualityControlReport对象的id属性
                    list.get(k - 1).setIdOnWork(String.valueOf(qcr.getId()));
                }
            }
        } else {
            for (int n = 1; n <= 8; n++) {
                if (isEffectiveDate(createTime, n, "冰箱")) {
                    list.get(n - 1).setTime((n - 1) * 3 + "-" + n * 3);
                    //设备下班巡检的运行状态
                    list.get(n - 1).setRunningStatusOffWork(qcr.getRunningStatus());
                    //下班巡检的创建时间
                    list.get(n - 1).setInspectionTimeOffWork(qcr.getCreateTime());
                    //下班巡检的巡检人
                    list.get(n - 1).setCheckingPeopleOffWork(qcr.getCreateBy());
                    //下班巡检的冷藏温度
                    list.get(n - 1).setColdTemperatureOffWork(qcr.getColdTemperature());
                    //如果是一号冰箱和5号冰箱，设置冷冻温度，其他冰箱没有冷冻温度
                    if (StringUtils.equals("1号冰箱", qcr.getDeviceName()) ||
                            StringUtils.equals("海尔医用冷藏冷冻箱5", qcr.getDeviceName())) {
                        //下班巡检的冷冻温度
                        list.get(n - 1).setFreezingTemperatureOffWork(qcr.getFreezingTemperature());
                    }
                    //将当前qcr对象的巡检记录id赋值给QualityControlReport对象的id属性
                    list.get(n - 1).setIdOffWork(qcr.getId());
                }
            }
        }
    }

    /**
     * 根据培养箱上下班状态设置属性
     *
     * @param qcr        QcEquipmentRecordModel
     * @param createTime 创建时间
     */
    public static void determineSetPropertiesForPYX(QcEquipmentRecordModel qcr, Date createTime, List<QualityControlReport> list) {
        //上班状态
        if (StringUtils.equals("1", qcr.getWorkStatus())) {
            for (int k = 1; k <= 4; k++) {
                if (isEffectiveDate(createTime, k, "培养箱")) {
                    list.get(k - 1).setTime((k - 1) * 6 + "-" + k * 6);
                    list.get(k - 1).setRunningStatusOnWork(qcr.getRunningStatus());
                    list.get(k - 1).setInspectionTimeOnWork(qcr.getCreateTime());
                    list.get(k - 1).setCheckingPeopleOnWork(qcr.getCreateBy());
                    list.get(k - 1).setIdOnWork(String.valueOf(qcr.getId()));
                }
            }
        } else {
            for (int n = 1; n <= 4; n++) {
                if (isEffectiveDate(createTime, n, "培养箱")) {
                    list.get(n - 1).setTime((n - 1) * 6 + "-" + n * 6);
                    list.get(n - 1).setRunningStatusOffWork(qcr.getRunningStatus());
                    list.get(n - 1).setInspectionTimeOffWork(qcr.getCreateTime());
                    list.get(n - 1).setCheckingPeopleOffWork(qcr.getCreateBy());
                    list.get(n - 1).setIdOffWork(qcr.getId());
                }
            }
        }
    }

    /**
     * 判断当前时间是否在nowTime这一天的某个区间内，每3个小时一个区间
     *
     * @param nowTime 创建时间
     * @param str     表示在哪一个时间区间，冰箱内1代表0-3，2代表3-6依次递增，培养箱1代表0-6，2代表6-12依次递增
     * @param eqName  设备类型名称，表示进行判断的是哪一种设备，有冰箱，培养箱等
     * @return
     */
    public static boolean isEffectiveDate(Date nowTime, int str, String eqName) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);
        Calendar begin = Calendar.getInstance();
        begin.setTime(nowTime);
        Calendar end = Calendar.getInstance();
        end.setTime(nowTime);
        begin.set(Calendar.MINUTE, 0);
        begin.set(Calendar.SECOND, 0);
        end.set(Calendar.MINUTE, 59);
        end.set(Calendar.SECOND, 59);
        if (StringUtils.equals("冰箱", eqName)) {
            switch (str) {
                case 1:
                    begin.set(Calendar.HOUR_OF_DAY, 0);
                    end.set(Calendar.HOUR_OF_DAY, 2);
                    break;
                case 2:
                    begin.set(Calendar.HOUR_OF_DAY, 3);
                    end.set(Calendar.HOUR_OF_DAY, 5);
                    break;
                case 3:
                    begin.set(Calendar.HOUR_OF_DAY, 6);
                    end.set(Calendar.HOUR_OF_DAY, 8);
                    break;
                case 4:
                    begin.set(Calendar.HOUR_OF_DAY, 9);
                    end.set(Calendar.HOUR_OF_DAY, 11);
                    break;
                case 5:
                    begin.set(Calendar.HOUR_OF_DAY, 12);
                    end.set(Calendar.HOUR_OF_DAY, 14);
                    break;
                case 6:
                    begin.set(Calendar.HOUR_OF_DAY, 15);
                    end.set(Calendar.HOUR_OF_DAY, 17);
                    break;
                case 7:
                    begin.set(Calendar.HOUR_OF_DAY, 18);
                    end.set(Calendar.HOUR_OF_DAY, 20);
                    break;
                case 8:
                    begin.set(Calendar.HOUR_OF_DAY, 21);
                    end.set(Calendar.HOUR_OF_DAY, 0);
                    end.set(Calendar.MINUTE, 0);
                    end.set(Calendar.SECOND, 0);
                    end.add(Calendar.DATE, 1);
                    break;
            }
        } else if (StringUtils.equals("培养箱", eqName) || StringUtils.equals("环境", eqName)) {
            switch (str) {
                case 1:
                    begin.set(Calendar.HOUR_OF_DAY, 0);
                    end.set(Calendar.HOUR_OF_DAY, 5);
                    break;
                case 2:
                    begin.set(Calendar.HOUR_OF_DAY, 6);
                    end.set(Calendar.HOUR_OF_DAY, 11);
                    break;
                case 3:
                    begin.set(Calendar.HOUR_OF_DAY, 12);
                    end.set(Calendar.HOUR_OF_DAY, 17);
                    break;
                case 4:
                    begin.set(Calendar.HOUR_OF_DAY, 18);
                    end.set(Calendar.HOUR_OF_DAY, 0);
                    end.set(Calendar.MINUTE, 0);
                    end.set(Calendar.SECOND, 0);
                    end.add(Calendar.DATE, 1);
                    break;
            }
        }
        if (date.after(begin) && date.before(end)) {
            return true;
        } else if (date.compareTo(begin) == 0 || date.compareTo(end) == 0) {
            return true;
        } else {
            return false;
        }
    }
}
